﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QUT.Bio.SilverMap.ObjectModel;
using QUT.Bio.Util;
using System.Linq;
using System.ComponentModel;

namespace QUT.Bio.SilverMap {
	/// <summary>
	/// A HitSummary object presents a flattened property collection that covers
	/// details of the Hit itself, whether or not the Hit is presently selected in 
	/// the model's SelectedHits list, plus information about the referenced 
	/// subject sequence.
	/// <para>
	///		Most of a HitSummary is immutable; however, the 'selected' property  can
	///		be changed to indicate inclusion of the hit in the current selection.
	/// </para>
	/// </summary>

	public class HitSummary: INotifyPropertyChanged {

		private ViewModel model;
		private Hit hit;
		private ObjectWrapper hitSequenceWrapper;
		private bool selected;
		
		public HitSummary (
			ViewModel model,
			Hit hit
		) {
			this.model = model;
			this.hit = hit;
			hitSequenceWrapper = new ObjectWrapper( hit.HitFeature.ContainingSequence );
		}
		
		public Hit Hit {
			get {
				return hit;
			}
		}
		
		public bool Selected {
			get {
				return selected;
			}
			set {
				if ( value != selected ) {
					selected = value;
					
					if ( PropertyChanged != null ) {
						PropertyChanged( this, new PropertyChangedEventArgs( "Selected" ) );
					}
				}
			}
		}
		
		public string Name {
			get {
				return hit.HitFeature.ContainingSequence.Name;
			}
		}
		
		public IFeature QueryFeature {
			get {
				return hit.QueryFeature;
			}
		}
		
		public IFeature HitFeature {
			get {
				return hit.HitFeature;
			}
		}

		public long QueryFrom {
			get {
				return hit.QueryFeature.Start;
			}
		}
		
		public string QuerySequence {
			get {
				return hit.QuerySequence;
			}
		}

		public long QueryTo {
			get {
				return hit.QueryFeature.End;
			}
		}

		public long HitFrom {
			get {
				return hit.HitFeature.Start;
			}
		}
		
		public string HitSequence {
			get {
				return hit.HitSequence;
			}
		}

		public long HitTo {
			get {
				return hit.HitFeature.End;
			}
		}
		
		public string Product {
			get {
				return hitSequenceWrapper.HasProperty( "Product" ) 
					? hitSequenceWrapper["Product"].ToString() 
					: "n/a";
			}
		}
		
		public string GI {
			get {
				var xrefs = from xref in hit.HitFeature.ContainingSequence.Xref
							where xref.Database.Equals( "GI", StringComparison.InvariantCultureIgnoreCase )
							select xref.Id;
				
				string result = xrefs.FirstOrDefault();
				
				if ( result == null ) {
					result = "n/a";
				}
				
				return result;
			}
		}
		
		public string Midline {
			get {
				return hit.Midline;
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;
		
		public IEnumerable<Xref> Xref {
			get {
				return hit.HitFeature.ContainingSequence.Xref;
			}
		}
		
		public double EValue {
			get {
				return hit.EValue;
			}
		}
		
		public long Identities {
			get {
				return hit.Identities;
			}
		}
		
		public long Positives {
			get {
				return hit.Positives;
			}
		}
	}
}
